package com.jlboot.app.admin.util;

import com.jlboot.core.util.JwtUtil;
import com.jlboot.core.util.StringUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 51594 on 2020/2/26.
 */
public class ValidateRole {

    public String jwt = "";

    public static ValidateRole n(String jwt) {
        return new ValidateRole(jwt);
    }


    public static ValidateRole n(HttpServletRequest request) {
        String jwt = null;
        return new ValidateRole(jwt);
    }

    public ValidateRole(String jwt) {
        this.jwt = jwt;
    }

    public boolean v(String pass_roles) {
        boolean has_roles = false;
        JwtUtil.JwtSubject js=JwtUtil.getJwtInfo(jwt);
        String user_roles = js.getRoles() + "|login|";
        if (user_roles != null) {
            if (user_roles.indexOf("|administrator|") > -1) {//超级用户全通过
                has_roles = true;
            } else {
                String[] user_roless = StringUtil.split(user_roles, "|");
                for (int k = 0; k < user_roless.length; k++) {
                    String role = user_roless[k];
                    if (role != null && role.length() > 0 && pass_roles.indexOf("|" + role + "|") > -1) {
                        has_roles = true;
                        break;
                    }
                }
            }
        }

        return has_roles;
    }
}
